<template>
  <div>
    <e-table
      @getList="handleGetList"
      :tableData="tableData"
      :columns="columns"
      :loading="loading"
      :total="total"
    >
      <el-table-column slot="state" width="80" align="center" label="状态">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.state === 0">正常</el-tag>
          <el-tag type="danger" v-else>禁用</el-tag>
        </template>
      </el-table-column>
    </e-table>
  </div>
</template>

<script>
import { queryDeptUserList } from '@/api/department'

export default {
  data() {
    return {
      loading: false,
      // 查询参数
      queryParam: {
        deptId: '',
        pageNo: 1,
        pageSize: 10
      },
      // 表头
      columns: [
        {
          label: '姓名',
          prop: 'name'
        },
        {
          label: '用户名',
          prop: 'username'
        },
        {
          label: '手机号',
          prop: 'phone'
        },
        {
          label: '所属部门岗位',
          prop: 'deptName'
        },
        { slot: 'state' },
        {
          slot: 'action'
        }
      ],
      total: 1,
      tableData: []
    }
  },
  mounted() {
    this.handleGetList()
  },
  methods: {
    getId(obj) {
      this.queryParam.deptId = obj.parentId
      this.handleGetList()
    },
    handleEdit(row) {
      this.$nextTick(() => {
        this.$emit('edit', row)
      })
    },
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除[${row.menuName}]这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await deleteMenu(row.menuId, {})
          _this.$message.success('删除成功')
          this.handleGetList()
        }
      })
    },
    async handleGetList(param = {}) {
      this.loading = true
      this.queryParam = Object.assign(this.queryParam, param)
      const res = await queryDeptUserList(this.queryParam)
      if (res.data && res.data.records.length > 0) {
        const {
          state,
          data: { records, total }
        } = res
        this.tableData = records
        this.total = total
      } else {
        this.tableData = []
        this.total = 1
      }
      this.loading = false
    }
  }
}
</script>
